
module Cms
  class PageLayout < Document

    def initialize
      super()
      self.style = ";width:95%"
      self.draggable = false

      @header = LayoutPortion.new(:style => 'clear:both; min-height:2em; width:98%')
      @header.draggable = false
      @header.portion_name = 'header'

      @sidebar = LayoutPortion.new(:style => 'width:28%; float:left')
      @sidebar.draggable = false
      @sidebar.portion_name = 'sidebar'

      @content = LayoutPortion.new(:style => 'width:68%')
      @content.draggable = false
      @content.portion_name = 'content'

      @footer = LayoutPortion.new(:style => 'clear:both; min-height:2em; width:98%')
      @footer.draggable = false
      @footer.portion_name = 'footer'
    end

    def get_state
      super.merge(:droppable => false)
    end

    def set_children_states states
      portions = {}
      states.each { |state|
        portions[state[0]['portion_name']] = state
      }
      @header.set_state portions['header']
      @sidebar.set_state portions['sidebar']
      @content.set_state portions['content']
      @footer.set_state portions['footer']
    end

    def build
      super().tap {
        add @header
        add @sidebar
        add @content
        add @footer
      }
    end

  end

  class LayoutPortion < Document
    state_defined_by :portion_name => nil
  end
end